package types

import (
	"context"
	"github.com/zeromicro/go-zero/core/trace"
)

type EmptyResponseData struct {
}

// 自定义错误结构体
type MyError struct {
	Code int64  `json:"code"`
	Msg  string `json:"msg"`
}

// 实现 Error() 方法
func (e *MyError) Error() string {
	return e.Msg
}

// 创建自定义错误
func NewMyError(code int64, msg string) *MyError {
	return &MyError{
		Code: code,
		Msg:  msg,
	}
}

func SuccessResponseEmpty(ctx context.Context) *CommonResponse {
	return &CommonResponse{
		//Code:    constants.CodeSuccess.Code, //import cycle not allowed
		Code:    0,
		Msg:     "Success",
		TraceId: trace.TraceIDFromContext(ctx),
		Data:    EmptyResponseData{},
	}
}

func SuccessResponse(data interface{}, ctx context.Context) *CommonResponse {
	return &CommonResponse{
		//Code:    constants.CodeSuccess.Code, //import cycle not allowed
		Code:    0,
		Msg:     "Success",
		TraceId: trace.TraceIDFromContext(ctx),
		Data:    data,
	}
}
func ErrorResponse(code int64, msg string, ctx context.Context) *CommonResponse {
	return &CommonResponse{
		Code:    code,
		Msg:     msg,
		TraceId: trace.TraceIDFromContext(ctx),
		Data:    nil,
	}
}

func Fail(err *MyError) *CommonResponse {
	return &CommonResponse{
		Code: err.Code,
		Msg:  err.Msg,
		Data: nil,
	}
}
